package juniors.servlet.idea;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import juniors.dao.IdeaDAO;
import juniors.excepcion.DAOExcepcion;
import juniors.modelo.Idea;
import juniors.negocio.GestionIdeas;

/**
 * Servlet implementation class IdeListadoServlet
 */
@WebServlet("/IdeListadoServlet")
public class IdeListadoServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IdeListadoServlet() {
        super();
        System.out.println("servlet");
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stubSystem.out.println("get");
		String titulo = request.getParameter("nombre");
		System.out.println(titulo);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("post");
		String titulo = request.getParameter("nombre");
		String estado = request.getParameter("estado");
		System.out.println(titulo);
			Idea idea = new Idea();
			IdeaDAO dao = new IdeaDAO();
			//dao.buscarIdea(titulo, "ss");
			GestionIdeas negocio = new GestionIdeas();
			try {
				Collection<Idea> lista = negocio.buscarIdea(titulo, estado);
				System.out.println("Iniciar busqueda");
					for(Idea x : lista)	{
						System.out.println(x.getTitulo()+" "+x.getDescripcion());
						
					}
					System.out.println("fin busqueda");	
				// Guardar en el ambiente de request
				request.setAttribute("ideaListado", lista);
				RequestDispatcher rd = request.getRequestDispatcher("idea_buscar.jsp");
				rd.forward(request, response);

			} catch (DAOExcepcion e) {
				System.out.println(e.getMessage());
				RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
				rd.forward(request, response);
			}
			}

		}